package com.central.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.central.common.feign.DepartmentService;
import com.central.common.feign.ProjectService;
import com.central.common.model.*;
import com.central.common.service.impl.SuperServiceImpl;
import com.central.project.mapper.SysProjectMapper;
import com.central.common.model.SysProject;
import com.central.project.service.ISysProjectService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author Tindy
 * @date 2021/6/24
 * @describe
 */
@Slf4j
@Service
public class SysProjectServiceImpl extends SuperServiceImpl<SysProjectMapper, SysProject> implements ISysProjectService {
    @Autowired
    private DepartmentService departmentService;

    @Override
    public PageResult<SysProject> findProjects(Map<String, Object> params) {
        Page<SysProject> page = new Page<>(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"));
        List<SysProject> list = baseMapper.findProjects(page, params);
        Map<String, SysDepartment> departmentMap = departmentService.findDepartmentMap();
        list.parallelStream().forEach(t->t.setDepartment(departmentMap.get(t.getDepartmentId().toString()).getName()));
        long total = page.getTotal();
        return PageResult.<SysProject>builder().data(list).code(0).count(total).build();
    }

    @Override
    public PageResult<SysProject> findUserProjects(Long userId, Map<String, Object> params) {
        Page<SysProject> page = new Page<>(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"));
        List<SysProject> list = baseMapper.findUserProjects(page, userId,params);
        Map<String, SysDepartment> departmentMap = departmentService.findDepartmentMap();
        list.parallelStream().forEach(t->t.setDepartment(departmentMap.get(t.getDepartmentId().toString()).getName()));
        long total = page.getTotal();
        return PageResult.<SysProject>builder().data(list).code(0).count(total).build();
    }

    @Override
    public List<SysProject> findProByProIds(List<Long> proIds) {
        proIds.add(-1l);
        return baseMapper.findProByProIds(proIds);
    }

    @Override
    public List<SysProject> selectLikeName(String name) {
        return baseMapper.selectList(new QueryWrapper<SysProject>().like("name",name));
    }

    @Override
    public Result create(SysProject project) {
        project.setId(null);
        if(!project.getName().matches("[0-9A-Za-z]*")) return  Result.failed("操作失败:项目名仅由字母或数字组成");
        save(project);
        return Result.succeed("操作成功");
    }

    @Override
    public Integer countByDepartmentId(Long departmentId) {
        return baseMapper.selectCount(new QueryWrapper<SysProject>().eq("department_id", departmentId));
    }

}
